package TYURIN;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Search extends TYURIN.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testSearch() throws Exception {
	driver.get(baseUrl + "/php4dvd");
    WebDriverWait wait = new WebDriverWait(driver, 30);
/*   
    driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys("admin");
    driver.findElement(By.name("password")).clear();
    driver.findElement(By.name("password")).sendKeys("admin");
    driver.findElement(By.name("submit")).click(); 
*/    
    driver.findElement(By.id("q")).clear();
    driver.findElement(By.id("q")).sendKeys("Terminator");
    driver.findElement(By.id("q")).sendKeys(Keys.RETURN);
    Thread.sleep(2000);
    WebElement element2 = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.title")));
    assertEquals("Terminator", driver.findElement(By.cssSelector("div.title")).getText());    

    driver.findElement(By.id("q")).clear();
    driver.get(baseUrl + "/php4dvd");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.findElement(By.id("q")).clear();
    driver.findElement(By.id("q")).sendKeys("Flight of navigator");
    driver.findElement(By.id("q")).sendKeys(Keys.RETURN);
    Thread.sleep(1000);
    WebElement element3 = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[1]/div/div/section/div[3]/a[1]/div/div[2]")));
    assertEquals("Flight of the Navigator", driver.findElement(By.cssSelector("div.title")).getText());
    
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
